package cn.kgc.eduservice.controller;


import cn.kgc.commonutils.R;
import cn.kgc.eduservice.entity.EduCourse;
import cn.kgc.eduservice.entity.EduTeacher;
import cn.kgc.eduservice.entity.vo.CourseInfoVo;
import cn.kgc.eduservice.entity.vo.CoursePublishVo;
import cn.kgc.eduservice.entity.vo.CourseQuery;
import cn.kgc.eduservice.entity.vo.TeacherQuery;
import cn.kgc.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-06
 */
@Api(tags="课程管理")
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;
    //课程列表
    @ApiOperation(value = "所有课程列表")
    @GetMapping
    public R allCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);

    }
    @ApiOperation(value = "分页课程列表")
    @GetMapping("pageCourse/{current}/{limit}")
    public R pageListTeacher (@ApiParam(name = "current", value = "当前页数", required = true)@PathVariable Long current,
                              @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit){
        // 创建page对象
        Page<EduCourse> coursePage = new Page<>(current,limit);
        // 调用 方法来实现分页
        courseService.page(coursePage, null);

        Long total = coursePage.getTotal(); //总记录数
        List<EduCourse> records = coursePage.getRecords();// 数据list 集合

        return R.ok().data("totals",total).data("rows",records);
    }
    // 条件查询带分页的方法
    @ApiOperation(value = "分页条件查询课程列表")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current", value = "当前页数", required = true)@PathVariable Long current,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit,
                                  @RequestBody(required = false) CourseQuery courseQuery){
        // 创建page对象
        Page<EduCourse> coursePage= new Page<>(current,limit);
        //构件条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        String title = courseQuery.getTitle();

        String status = courseQuery.getStatus();
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }

        courseService.page(coursePage, wrapper);

        Long total = coursePage.getTotal(); //总记录数
        List<EduCourse> records = coursePage.getRecords();// 数据list 集合
        return R.ok().data("total",total).data("rows",records);
    }
    //添加课程基本信息的方法
    @ApiOperation(value = "新增课程")
    @PostMapping("addCourseInfo")
    public R addCourseInfo (@RequestBody CourseInfoVo courseInfoVo){
       String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程查询课程基本信息
    @ApiOperation(value = "根据课程查询课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourseInfo")
    public  R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return  R.ok();
    }
    //根据课程id查询课程确认信息
    @ApiOperation(value = "根据id发布课程")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo( @ApiParam(name = "id", value = "课程ID", required = true)
             @PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.publistCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }
    //课程最终发布
    // 修改课程状态
    @ApiOperation(value = "根据id修改课程状态")
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }
    @ApiOperation(value = "删除课程")
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }
}

